package com.franklin.java.api.spring.annotation.bean;

import com.franklin.plugins.common.collection.PrettyHashSet;
import com.franklin.java.api.spring.annotation.SpringAnnotation;
import com.franklin.plugins.common.utils.DataCheckUtils;

import java.util.Collections;
import java.util.Objects;
import java.util.Set;


/**
 * @author Franklin
 * @since 2021/8/13 14:11
 */
public abstract class BeanAnnotation extends SpringAnnotation {

    protected Set<String> beanNames;

    protected BeanAnnotation(String qualifiedName, String shortName, Set<String> beanNames) {
        super(qualifiedName, shortName, null);
        this.beanNames = DataCheckUtils.isEmpty(beanNames) ? Collections.emptySet() : beanNames;
    }

    public Set<String> getBeanNames() {
        return beanNames;
    }

    public boolean match(String beanName) {
        if (DataCheckUtils.isEmpty(beanNames)) {
            return false;
        }
        return beanNames.contains(beanName);
    }

    protected Set<String> addSuffix(Set<String> beanNames){
        String suffix = getSuffix();
        if (Objects.nonNull(suffix)){
            Set<String> newBeanNames = new PrettyHashSet<>();
            beanNames.forEach(beanName -> newBeanNames.add(beanName + suffix));
            return newBeanNames;
        }
        return beanNames;
    }

    protected String getSuffix(){
        return null;
    }
}
